package com.hnhd.eorder.db;

import java.util.ArrayList;

import android.content.ContentValues;
import android.database.Cursor;

import com.hnhd.eorder.vo.OrderItem;

/**
 * 订单子项数据操作类
 * 
 * @author peng
 * 
 */
public class OrderItemUtil {

	public static OrderItem getOrderItemById(String id) {
		Cursor cursor = SQLUtil.query(SQLUtil.ORDERITEM, null, "id=?",
				new String[] { id }, null, null, null);
		OrderItem orderItem = null;
		if (cursor.moveToFirst()) {
			do {
				orderItem = new OrderItem();
				orderItem.setId(cursor.getInt(0));
				orderItem.setOrderID(cursor.getInt(1));
				orderItem.setDishID(cursor.getInt(2));
				orderItem.setNumber(cursor.getInt(3));
				orderItem.setPrice(cursor.getFloat(4));
				orderItem.setDiscount(cursor.getFloat(5));
				orderItem.setRemarks(cursor.getString(6));
				orderItem.setStatus(cursor.getString(7));
				orderItem.setIsPrint(cursor.getInt(8));
			} while (cursor.moveToNext());
		}
		if (cursor != null && !cursor.isClosed()) {
			cursor.close();
		}
		return orderItem;
	}

	public static ArrayList<OrderItem> getAllOrderItems() {
		Cursor cursor = SQLUtil.getReadableDatabase().rawQuery(
				"SELECT * FROM " + SQLUtil.ORDERITEM, null);
		ArrayList<OrderItem> orderItems = new ArrayList<OrderItem>();
		if (cursor.moveToFirst()) {
			do {
				OrderItem orderItem = new OrderItem();
				orderItem.setId(cursor.getInt(0));
				orderItem.setOrderID(cursor.getInt(1));
				orderItem.setDishID(cursor.getInt(2));
				orderItem.setNumber(cursor.getInt(3));
				orderItem.setPrice(cursor.getFloat(4));
				orderItem.setDiscount(cursor.getFloat(5));
				orderItem.setRemarks(cursor.getString(6));
				orderItem.setStatus(cursor.getString(7));
				orderItem.setIsPrint(cursor.getInt(8));
				orderItems.add(orderItem);
			} while (cursor.moveToNext());
		}
		if (cursor != null && !cursor.isClosed()) {
			cursor.close();
		}
		return orderItems;
	}

	public static long insertOrderItem(OrderItem orderItem) {
		ContentValues values = new ContentValues();
		values.put("id", orderItem.getId());
		values.put("orderId", orderItem.getOrderID());
		values.put("dishId", orderItem.getDishID());
		values.put("number", orderItem.getNumber());
		values.put("price", orderItem.getPrice());
		values.put("discount", orderItem.getDiscount());
		values.put("remarks", orderItem.getRemarks());
		values.put("status", orderItem.getStatus());
		values.put("isPrint", orderItem.getIsPrint());
		return SQLUtil.getWritableDatabase().insert(SQLUtil.ORDERITEM, null,
				values);
	}

	public static long updateOrderItem(OrderItem orderItem) {
		ContentValues values = new ContentValues();
		values.put("id", orderItem.getId());
		values.put("orderId", orderItem.getOrderID());
		values.put("dishId", orderItem.getDishID());
		values.put("number", orderItem.getNumber());
		values.put("price", orderItem.getPrice());
		values.put("discount", orderItem.getDiscount());
		values.put("remarks", orderItem.getRemarks());
		values.put("status", orderItem.getStatus());
		values.put("isPrint", orderItem.getIsPrint());
		return SQLUtil.getWritableDatabase().update(SQLUtil.ORDERITEM, values,
				"id=?", new String[] { String.valueOf(orderItem.getId()) });
	}

	public static void deleteOrderItem(OrderItem orderItem) {
		SQLUtil.getWritableDatabase().delete(SQLUtil.ORDERITEM, "id=?",
				new String[] { String.valueOf(orderItem.getId()) });
	}
	

	public static void deleteAllOrderItems() {
		SQLUtil.getWritableDatabase().delete(SQLUtil.ORDERITEM, null,null);
	}
}
